import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/common/res/gaps.dart';
import 'package:oramemo/common/styles/colors.dart';
import 'package:oramemo/common/utils/time_util.dart';

class PlayerWidget extends StatelessWidget {
  final bool isPlaying;

  final Duration duration;

  final Duration position;

  final Function() onTap;

  const PlayerWidget({
    super.key,
    required this.isPlaying,
    required this.duration,
    required this.position,
    required this.onTap,
  });

  String _formatDuration(Duration duration) {
    return TimeUtils.formatDuration(duration);
  }

  double _calculateProgress(Duration position, Duration duration) {
    if (duration.inMilliseconds == 0) return 0.0;
    return position.inMilliseconds / duration.inMilliseconds;
  }

  @override
  Widget build(BuildContext context) {
    String positStr = _formatDuration(position);
    String durationStr = _formatDuration(duration);
    double progress = _calculateProgress(position, duration);
    return Row(
      children: [
        GestureDetector(
          onTap: onTap,
          child: IconX.icon(
            isPlaying ? Icons.pause_circle : Icons.play_circle,
            color: Colors.black,
            size: 60,
          ),
        ),
        const SizedBox(width: 19, height: 19),
        SizedBox(
          width: Get.width - 118 - 36,
          child: Column(
            children: [
              LinearProgressIndicator(
                value: progress,
                valueColor: const AlwaysStoppedAnimation(Color(0xFF65ECCF)),
                borderRadius: const BorderRadius.all(Radius.circular(5)),
                backgroundColor: const Color(0xffF3F6F9),
                minHeight: 44,
              ),
              Gaps.vGap10,
              Row(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    positStr,
                    style: const TextStyle(fontSize: 16, color: Colours.text),
                  ),
                  Text(
                    durationStr,
                    style: const TextStyle(fontSize: 16, color: Colours.text),
                  ),
                ],
              ),
            ],
          ),
        ),
      ],
    );
  }
}
